#ifndef CODING_INTERVIEWS_TREENODE_H
#define CODING_INTERVIEWS_TREENODE_H

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x, TreeNode *left = nullptr, TreeNode *right = nullptr) : val(x), left(left), right(right) {}

    friend std::ostream &operator<<(std::ostream &out, TreeNode *node) {
        if (node == nullptr) {
            out << "#";
        } else {
            out << "(" << node->left << ", " << node->val << ", " << node->right << ")";
        }
        return out;
    }
};

TreeNode *buildTree(int val, TreeNode *left = nullptr, TreeNode *right = nullptr) {
    return new TreeNode(val, left, right);
}

void freeTree(TreeNode *node) {
    if (node == nullptr) {
        return;
    }
    freeTree(node->left);
    freeTree(node->right);
    delete node;
}

#endif //CODING_INTERVIEWS_TREENODE_H
